<template>
  <div class="main-box">
    <div class="table-box">
      <div class="card mb10 pt0 pb0">
        <SelectFilter :data="selectFilterData" :default-values="selectFilterValues" @change="changeSelectFilter" />
      </div>
      <ProTable
        ref="proTable"
        title="荆洲中学分站 配送员任务单列表"
        highlight-current-row
        :columns="columns"
        :request-api="getTaskOrderList"
        :init-param="Object.assign(initParam, selectFilterValues)"
      >
        <!-- 表格 header 按钮 -->
        <template #tableHeader>
          <el-button type="primary" :icon="Download" plain @click="downloadFile">导出任务单数据</el-button>
        </template>
        <!-- 表格操作 -->
        <template #operation="scope">
          <el-button type="primary" link :icon="View" @click="taskOrderDetail(scope.row, true)">查看</el-button>
          <el-button
            v-if="
              (scope.row.taskStatus === 3 && scope.row.taskType === 1) ||
              (scope.row.taskStatus === 2 && scope.row.taskType === 2) ||
              (scope.row.taskStatus === 3 && scope.row.taskType === 3)
            "
            type="primary"
            link
            :icon="EditPen"
            @click="taskOrderDetail(scope.row, false)"
            >回执填写</el-button
          >
          <!-- <el-button
            v-if="scope.row.taskType === 1 && scope.row.taskStatus != 4"
            type="primary"
            link
            :icon="Check"
            @click="returnToSubstation(scope.row)"
            >退货到站</el-button
          >
          <el-button
            v-if="scope.row.taskType === 1 && scope.row.taskStatus != 4"
            type="primary"
            link
            :icon="Check"
            @click="exchangeToSubstation(scope.row)"
            >换货到站</el-button
          >
          <el-button v-if="scope.row.taskStatus != 4" type="primary" link :icon="EditPen" @click="doChangeTaskStatus(scope.row)"
            >编辑状态</el-button
          > -->
        </template>
      </ProTable>
      <!-- <StatusDialog ref="statusRef" /> -->
      <DetailDrawer ref="detailRef" />
      <ImportExcel ref="dialogRef" />
    </div>
  </div>
</template>
<script setup lang="tsx" name="useSelectFilter">
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import ImportExcel from "@/components/ImportExcel/index.vue";
import SelectFilter from "@/components/SelectFilter/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { EditPen, Download, View } from "@element-plus/icons-vue";
import { getTaskOrderList, editTaskOrder, exportTaskOrderInfo } from "@/api/modules/substation";
import { Substation } from "@/api/interface";
import { taskStatus, taskType } from "@/utils/serviceDict";
import { useDownload } from "@/hooks/useDownload";
// import StatusDialog from "@/views/substation/taskOrder/components/StatusDialog.vue";
import DetailDrawer from "@/views/substation/taskOrder/components/DetailDrawer.vue";
// import { useHandleData } from "@/hooks/useHandleData";

// 获取 ProTable 元素，调用其获取刷新数据方法（还能获取到当前查询参数，方便导出携带参数）
const proTable = ref<ProTableInstance>();

// 表格配置项
const columns: ColumnProps[] = [
  { prop: "taskId", label: "单号", width: 90, sortable: true, search: { el: "input" } },
  { prop: "orderInfo.receiverName", label: "收货人", width: 80 },
  { prop: "orderInfo.receiverAddress.addressString", label: "收货地址" },
  { prop: "orderInfo.receiverPhone", label: "收货人电话", width: 120 },
  { prop: "taskType", label: "任务类型", width: 120, sortable: true, tag: true, enum: taskType },
  { prop: "taskStatus", label: "任务状态", width: 120, sortable: true, tag: true, enum: taskStatus },
  { prop: "orderInfo.expDeliveryTime", label: "期望送达时间", sortable: true, width: 200 },
  {
    prop: "taskStatus",
    label: "任务进度",
    render: scope => {
      return (
        <div>
          <el-progress percentage={Math.round((scope.row.taskStatus / 4) * 100)}></el-progress>
        </div>
      );
    },
    width: 160
  },
  { prop: "operation", label: "操作", fixed: "right", width: 200 }
];

// selectFilter 数据
const selectFilterData = reactive([
  {
    title: "任务类型(单)",
    key: "taskType",
    options: [
      {
        label: "全部",
        value: -1
      },
      {
        label: "送货收款",
        value: 1,
        icon: "Box"
      },
      {
        label: "退货",
        value: 2,
        icon: "SoldOut"
      },
      {
        label: "换货",
        value: 3,
        icon: "Sell"
      }
    ]
  },
  {
    title: "任务状态(多)",
    key: "taskStatus",
    multiple: true,
    options: [
      {
        label: "全部",
        value: -1
      },
      {
        label: "待领货",
        value: 2,
        icon: "Sell"
      },
      {
        label: "待配送",
        value: 3,
        icon: "Van"
      },
      {
        label: "已完成",
        value: 4,
        icon: "CircleCheck"
      }
    ]
  }
]);

// 初始化方法，挂载之后执行的东西填到里面
onMounted(() => {});

// 初始值，传入分站id和配送员id
const initParam = reactive({ substationId: 1, courierId: 9 });

// 默认 selectFilter 参数
const selectFilterValues = ref({ taskType: -1, taskStatus: [2, 3] });
const changeSelectFilter = (value: typeof selectFilterValues.value) => {
  ElMessage.success("筛选成功 😘");
  proTable.value!.pageable.current = 1;
  selectFilterValues.value = value;
};

// 导出任务单列表
const downloadFile = async () => {
  ElMessageBox.confirm("确认导出数据?", "温馨提示", { type: "warning" }).then(() =>
    useDownload(exportTaskOrderInfo, "任务单列表", proTable.value?.searchParam)
  );
};

const taskOrderDetail = (row: Substation.ResTaskOrderList, isView: boolean) => {
  openDetailDrawer(row, isView);
};

const detailRef = ref<InstanceType<typeof DetailDrawer> | null>(null);
// isView代表是否是查看任务单状态
const openDetailDrawer = (row: Partial<Substation.ResTaskOrderList> = {}, isView: boolean) => {
  const params = {
    title: "任务单详情",
    isView: isView,
    row: { ...row },
    api: editTaskOrder,
    getTableList: proTable.value?.getTableList
  };
  detailRef.value?.acceptParams(params);
};
</script>
